package com.army.study.ui.listcomponent;

import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.HashMap;
import java.util.Map;

public class CommentViewHolder {

    private Component convertView;

    private Map<Integer, Component> mViews = new HashMap<>();

    public CommentViewHolder(Component convertView) {
        this.convertView = convertView;
        this.convertView.setTag(this);
//        convertView.setTag(this);
    }

    public Component getConvertView() {
        return convertView;
    }

    public static CommentViewHolder getCommentViewHolder(Context context, Component convertView, int resource) {
        if (convertView == null) {
            return new CommentViewHolder(LayoutScatter.getInstance(context).parse(resource, null, false));
        }else {
            return (CommentViewHolder) convertView.getTag();
        }
    }

    public <T extends Component> T getView(int resId, Class<T> type) {
        Component view = mViews.get(resId);
        if (view == null) {
            view = convertView.findComponentById(resId);
            mViews.put(resId, view);
        }
        return (T)view;
    }

    public Text getTextView(int resId) {
        return getView(resId, Text.class);
    }

    public Image getImageView(int resId) {
        return getView(resId, Image.class);
    }
}
